package com.lognsoft.btest;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.ArrayList;

/*
 *    class FileOp
 */

class FileOp
{
public static boolean is_windows   = File.separatorChar == '\\';
public static String  _current_path = null;

public static String getPathOfFile(String s)
{
   int ind = s.lastIndexOf(File.separatorChar);
   return ind > 0 ? s.substring (0, ind + 1) : "";
};

public static boolean createPathOfFile (String s)
{
	File f= new File ( getPathOfFile(s) );
	
	if (f.exists())
	{
		return true;
	}
	
	return f.mkdirs();
}

public static String path_cat_as_string(String basePath, String name)
{
	StringOp.KeyValue kv = path_cat (basePath, name, false);
	return kv.key + kv.value;
}

public static StringOp.KeyValue path_cat(String basePath, String name, boolean check) 
{
	String FileName = 
		name.replace('\\', File.separatorChar)
			.replace('/',  File.separatorChar);

	if (is_windows && FileName.length() > 2
			&& FileName.charAt(1) == ':') 
	{
		/* through */
	} 
	else if (FileName.charAt(0) == File.separatorChar) 
	{
		/* through */
	} 
	else 
	{
		FileName = basePath + FileName;
	}

	File fd = new File(FileName);

	if (fd.exists()) 
	{
		FileName = fd.getAbsolutePath();
	}
	else if (check)
	{
		return null;
	}
	
	int ind = FileName.lastIndexOf(File.separatorChar);

	if (ind < 0) 
	{
		return new StringOp.KeyValue("", FileName);
	}
	else
	{
		return new StringOp.KeyValue ( 
			FileName.substring(0, ind + 1),
			FileName.substring(ind + 1) );
	}
}

public static String current_path()
{
	if (_current_path == null)
	{
		_current_path = new File(".").getAbsolutePath();
		
		while ( _current_path.endsWith(".") )
		{
			_current_path = _current_path.substring(0, _current_path.length() - 1);
		}		
	}
	
	return _current_path;
}

public static String relative_path(String path)
{
	String base_path = current_path();
	
	if (path.length() < base_path.length())
	{
		return path;
	}
	
	for (int i=0;i<base_path.length();i++)
	{
		if ( path.charAt(i) != base_path.charAt(i))
		{
			return path;
		};
	}
	
	base_path = path.substring( base_path.length() );
	
	return base_path.isEmpty() ? "." + File.separator : base_path;
}

public static ArrayList<File> listPath(File f)
{
	ArrayList<File> files = new ArrayList<File>();
	
	if (f.isDirectory() == false)
	{
		return files;
	}
	
	files.add(f);
	
	for (int i = 0; i<files.size(); i ++)
	{
		File[] sub = files.get(i).listFiles();
		
		if (sub == null)
		{
			continue;
		}

		for (File sf : sub)
		{
			if (sf.isDirectory() 
				&& sf.getName().equals(".") == false
				&& sf.getName().equals("..")== false)
			{
				files.add(sf);
			}
		}
	}
	
	return files;
}

public static boolean removePath(File f)
{
	if (f.exists() == false )
	{
		return true;
	}
	
	if (f.isDirectory() == false)
	{
		return f.delete();
	}
	
	ArrayList<File> files = new ArrayList<File>();
	boolean ret = true;

	files.add(f);
	
	for (int i = 0; i<files.size(); i ++)
	{
		File[] sub = files.get(i).listFiles();
		
		if (sub == null)
		{
			continue;
		}
		
		for (File sf : sub)
		{
			if (sf.isDirectory())
			{
				if ( sf.getName().equals(".") == false &&
					sf.getName().equals("..")== false)
				{
					files.add(sf);
				}
			}
			else
			{
				if ( false == sf.delete() )
				{
					ret = false;
				}
			}
		}
	}
	
	for (int i = files.size() - 1; i>=0; i--)
	{
		if ( files.get(i).delete() == false)
		{
			ret = false;
		}
	}
	
	return ret;
}

public static ArrayList<String>  getLinesFromFile (String filename) throws Exception
{
  ArrayList<String> lines = new ArrayList<String>();
  BufferedReader rd;

  if ( filename.startsWith("res://"))
  {
	  InputStream is = (new FileOp()).getClass().getResourceAsStream(filename.substring(6));
	  
	  if (is == null)
	  {
		  return lines;
	  }
	  
	  rd = new BufferedReader ( new InputStreamReader (is) );
  }
  else
  {
	  rd = new BufferedReader ( new FileReader (filename) );
  }
 
  String s;
  
  while ( (s = rd.readLine()) != null )
  {
	lines.add(s);
  }
  
  return lines;
};

};
